const express = require("express")
const router = express.Router()
const conn = require("../mysql")

// xxy
router.get("/hotShangPing", (req, res) => {
    let { hotPage } = req.query;
    console.log(req.query);
    let pageNum = (hotPage - 1) * 6;
    let result = {};
    const sql = `select * from hot_goods order by id desc limit ${pageNum},6`
    conn.query(sql, (err, data) => {
        if (err) {
            console.log(err);
        } else {
            if (data.length) {
                result = {
                    code: 1,
                    data
                }
            } else {
                result = {
                    code: 0
                }
            }
        }
        // res.send(`${callback}(${JSON.stringify(result)})`);
        res.send(result);

    })
})


// dog wmq
router.get('/dogGoods', (req, res) => {
    console.log(3333);
    let { page } = req.query
    console.log(req.query);
    let sql = `select * from dog_goods where type = ${page}`
    conn.query(sql, (err, data) => {
        if (err) {
            console.log(err);
        } else {
            if (data.length) {
                res.send({
                    code: 1,
                    data
                })
            } else {
                res.send({
                    code: 0
                })
            }
        }
    })
})

// 猫咪 la
router.get('/user', (req, res) => {
    let { tom } = req.query
    let sql = `select * from cat_goods where type='${tom}'`
        // console.log(sql)
    let result = {}
    conn.query(sql, (err, data) => {
        //错误
        if (err) {
            console.log('错误')
            return;
        }
        //查找到结果 [] [{}] [{},{},{}]
        if (data.length) {
            result = {
                code: 1,
                data
            }
        } else {
            result = {
                code: 0
            }
        }
        res.send(result)
    })

})


// 小宠 sfz
router.get('/search', (req, res) => {
    let { type } = req.query

    let sql = `select * from smallpet_goods where type='${type}'`
        // console.log(111);

    let result = {}
    conn.query(sql, (err, data) => {
        if (err) {
            console.log('错误')
            return;
        }
        if (data.length) {
            result = {
                code: 1,
                data
            }
        } else {
            result = {
                code: 0
            }
        }
        res.send(result)
    })

})

// 水族
router.get("/shuiZu", (req, res) => {
    let { type } = req.query;
    console.log(req.query);

    let result = {};
    const sql = `select * from fish_goods where type=${type}`
    conn.query(sql, (err, data) => {
        if (err) {
            console.log(err);
        } else {
            if (data.length) {
                result = {
                    code: 1,
                    data
                }
            } else {
                result = {
                    code: 0
                }
            }
        }
        // res.send(`${callback}(${JSON.stringify(result)})`);
        res.send(result);

    })
})

// 爬虫
router.get('/creeper', (req, res) => {
    console.log('get');

    let { type } = req.query;
    let sql = `select * from creeper where type='${type}'`
    conn.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            return;
        }
        if (data.length) {
            res.send({
                code: 1,
                data
            })
            return;
        }
        res.send({
            code: 0
        })

    })
})
router.get('/cart', (req, res) => {
    console.log('get');

    // let {type} = req.query;
    let sql = `select * from cart`
    conn.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            return;
        }
        if (data.length) {
            res.send({
                code: 1,
                data
            })
            return;
        }
        res.send({
            code: 0
        })

    })
})

// 关键字
router.get('/searchWords', (req, res) => {
    let age = req.query.age
    let sql = `select * from cat_mao1 where title like '%${age}%'`
    console.log('123' + age);
    console.log(sql);
    result = {}
    conn.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            return
        }
        if (data.length) {
            res.send({
                code: 1,
                data
            })
        } else {
            res.send({
                code: 0
            })
        }
    })
})
module.exports = router;